<template>
  <template v-show="visible">
    <!-- 使用Teleport把内容传送到body上渲染 -->
    <Teleport to="body">
      <div
        class="szy-dialog-overlay"
        @click="onClickOverlay"
        v-show="visible"
      ></div>
      <div class="szy-dialog-wrapper" v-show="visible">
        <div class="szy-dialog">
          <header>
            <slot name="title"></slot>
            <span class="szy-dialog-close" @click="close"></span>
          </header>
          <main>
            <slot name="content"></slot>
          </main>
          <footer>
            <Button @click="okFn">OK</Button>
            <Button @click="cancelFn">Cancel</Button>
          </footer>
        </div>
      </div>
    </Teleport>
  </template>
  <!-- <div v-show="visible">
    <Teleport to="body">
      <div class="szy-dialog-overlay" @click="onClickOverlay"></div>
      <div class="szy-dialog-wrapper">
        <div class="szy-dialog">
          <header>
            <slot name="title"></slot>
            <span class="szy-dialog-close" @click="close"></span>
          </header>
          <main>
            <slot name="content"></slot>
          </main>
          <footer>
            <Button @click="okFn">OK</Button>
            <Button @click="cancelFn">Cancel</Button>
          </footer>
        </div>
      </div>
    </Teleport>
  </div> -->
</template>

<script setup lang="ts">
import { toRefs } from "vue";
import Button from "./Button.vue";
// Dialog组件接收不同的props 为表现不同的组件样式
const props = defineProps({
  title: { type: String, default: "提示" },
  visible: { type: Boolean, default: false },
  closeOnClickOverlay: { type: Boolean, default: "normal" },
  ok: { type: Function },
  cancel: { type: Function },
});
const emit = defineEmits(["changeVisible"]);
const { visible, closeOnClickOverlay } = toRefs(props);
// console.log(props.visible);
const close = () => {
  emit("changeVisible");
  console.log(visible.value);
  // visibleVal.value = false;
  // console.log(visibleVal.value);
};
const onClickOverlay = () => {
  if (closeOnClickOverlay) {
    close();
  }
};
const okFn = () => {
  props.ok?.();
  close();
};
const cancelFn = () => {
  props.cancel?.();
  console.log(props.cancel);
  close();
  // alert("this is cancel function");
};
</script>

<style scoped lang="scss">
$radius: 4px;
$border-color: #d9d9d9;
.szy-dialog {
  background: white;
  border-radius: $radius;
  box-shadow: 0 0 3px fade_out(black, 0.5);
  min-width: 15em;
  max-width: 90%;
  &-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: fade_out(black, 0.5);
    z-index: 10;
  }
  &-wrapper {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 11;
  }
  > header {
    padding: 12px 16px;
    border-bottom: 1px solid $border-color;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 20px;
  }
  > main {
    padding: 12px 16px;
  }
  > footer {
    border-top: 1px solid $border-color;
    padding: 12px 16px;
    text-align: right;
  }
  &-close {
    position: relative;
    display: inline-block;
    width: 16px;
    height: 16px;
    cursor: pointer;
    &::before,
    &::after {
      content: "";
      position: absolute;
      height: 1px;
      background: black;
      width: 100%;
      top: 50%;
      left: 50%;
    }
    &::before {
      transform: translate(-50%, -50%) rotate(-45deg);
    }
    &::after {
      transform: translate(-50%, -50%) rotate(45deg);
    }
  }
  .szy-dialog-close {
    cursor: pointer;
  }
}
</style>
